//
//  MBPhoneNumView.m
//  Meboo
//
//  Created by kyo on 2023/11/21.
//

#import "MBPhoneNumView.h"

@interface MBPhoneNumView ()
<
UITextFieldDelegate
>

@property (nonatomic, strong) UILabel *zoneLab;
@property (nonatomic, strong) UITextField *phoneTF;
@end

@implementation MBPhoneNumView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Action

- (void)textFieldDidChange:(UITextField *)sender {
    if (sender.text.length >= 12) {
        sender.text = [sender.text substringToIndex:12];
    }
    sender.text = [self addSpacesToNumberString:sender.text];
}

- (NSString *)addSpacesToNumberString:(NSString *)numberString {
    // 移除字符串中的空格和其他非数字字符
    NSString *cleanString = [[numberString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
    if (self.phoneBlock) {
        self.phoneBlock(cleanString);
    }
    
    NSMutableString *resultString = [NSMutableString stringWithString:cleanString];
    
    NSString *forStr = [NSString stringWithFormat:@"%@",resultString];
    // 从后往前遍历字符串，每隔3位插入一个空格
    if (forStr.length >= 4) {
        for (NSInteger i = 3; i <= forStr.length; i += 4) {
            if (i == 3 || i == 7) {
                [resultString insertString:@" " atIndex:i];
            }
        }
    }
    
    return resultString;
}

#pragma mark - UI
- (void)setupUI {
    
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = MBHeightScale(5);
    self.layer.borderWidth = 1;
    self.layer.borderColor = HEXS(@"CDCDCD").CGColor;
    
    
    [self addSubview:self.zoneLab];
    [self.zoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(19));
        make.centerY.equalTo(self);
    }];
    
    [self addSubview:self.phoneTF];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(32));
        make.top.bottom.right.equalTo(self);
    }];
}

#pragma mark - Setter && Getter
- (UILabel *)zoneLab {
    if (!_zoneLab) {
        _zoneLab = [UILabel new];
        _zoneLab.text = @"+1";
        _zoneLab.textColor = HEXS(@"CDCDCD");
        _zoneLab.font = MBHelFontSizeScale(14);
    }
    return _zoneLab;
}

- (UITextField *)phoneTF {
    if (!_phoneTF) {
        _phoneTF = [[UITextField alloc] init];
        _phoneTF.backgroundColor = UIColor.whiteColor;
        _phoneTF.delegate = self;
        _phoneTF.tintColor = HEXS(@"333333");
//        [_phoneTF becomeFirstResponder];
        _phoneTF.placeholder = @"Phone Number";
        _phoneTF.font = MBHelBoldFontSizeScale(14);
        _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 2)];
        _phoneTF.leftViewMode = UITextFieldViewModeAlways;
        _phoneTF.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, -8, 2)];
        _phoneTF.rightViewMode = UITextFieldViewModeAlways;
        [_phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneTF;
}
@end
